package com.example.yuwei.mediaplayer.presenter;

import android.content.Context;

import com.example.yuwei.mediaplayer.module.MusicPlayer;
import com.example.yuwei.mediaplayer.timeutil.Tools;

import java.util.ArrayList;
import java.util.Map;
import java.util.Random;

public class MusicPresenter {
    private ArrayList<Map<String, Object>> musicList;
    private MusicPlayer musicPlayer;
    private int curPosition = 0;
    private final static int ORDER_PLAY = 0;
    private final static int RANDOM_PLAY = 1;
    private final static int LOOP_PLAY = 2;
    private static int play_Mode = ORDER_PLAY;
    private int currentPosition = 0;
    private String title;
    private static MusicPresenter musicPresenter;
    private String TAG = "musicPresenter";

    private MusicPresenter() {
        musicPlayer = MusicPlayer.getInstance();
    }

    public static MusicPresenter getInstance() {
        if (musicPresenter == null) {
            musicPresenter = new MusicPresenter();
        }
        return musicPresenter;
    }

    public ArrayList<Map<String, Object>> getMusicList(Context context) {
        Tools tools = new Tools();
        musicList = tools.scanAllAudioFiles(context);
        return musicList;
    }

    public void fisrtOpen() {
        Map<String, Object> musicMap = musicList.get(0);
        String url = musicMap.get("Url").toString();
        title = musicMap.get("Title").toString();
        musicPlayer.firstOpen(url);
    }

    public void playOrPause() {
        musicPlayer.pauseOrplay();
    }

    public void play(int curPosition) {
        this.curPosition = curPosition;
        Map<String, Object> musicMap = musicList.get(curPosition);
        String url = musicMap.get("Url").toString();
        title = musicMap.get("Title").toString();
        musicPlayer.play(url);
    }

    public void previous() {
        if (play_Mode == RANDOM_PLAY) {
            Random random = new Random();
            curPosition = random.nextInt(musicList.size());
        }
        if (play_Mode == ORDER_PLAY) {
            if (curPosition == 0) {
                curPosition = musicList.size() - 1;
            } else {
                curPosition--;
            }
        }
        play(curPosition);
    }

    public void next() {
        if (play_Mode == RANDOM_PLAY) {
            Random random = new Random();
            curPosition = random.nextInt(musicList.size());
        }
        if (play_Mode == ORDER_PLAY) {
            if (curPosition == musicList.size() - 1) {
                curPosition = 0;
            } else {
                curPosition++;
            }
        }
        play(curPosition);
    }

    public int setMode() {

        if (play_Mode < LOOP_PLAY) {
            play_Mode += 1;
        } else {
            play_Mode = ORDER_PLAY;
        }
        return play_Mode;
    }

    public String getTitle() {
        return title;
    }

    public void stop() {
        musicPlayer.stop();
    }

    public boolean isPlaying() {
        return musicPlayer.isPlaying();
    }

    public void setProgress(int progress) {
        this.currentPosition = progress;
    }

    public void progressPlay() {
        musicPlayer.progress(currentPosition);
    }

    public int getDuration() {
        return musicPlayer.getDuration();
    }

    public int getCurrentPosition() {

        return musicPlayer.getCurrentPosition();
    }
}
